The WebViewController
class allows you to display and interact with embedded web content inside your script. It is designed for use cases like custom in-app browsers, rendering dynamic HTML, or communicating with JavaScript running in a web context.
WebViewController
Represents a controller that manages a WebView.
shouldAllowRequest?: (request) => Promise<boolean>
An optional callback that determines whether to allow or block a request made by the WebView. This function is invoked before each resource is loaded, such as when navigating to a new page or submitting a form.
This is useful for intercepting navigation actions, implementing custom security logic, or filtering unwanted requests (e.g., ad domains).
The function receives a single request
object with the following properties:
url: string
The full URL of the request.
method: string
The HTTP method (e.g., GET
, POST
).
body?: Data | null
Optional body data sent with the request (e.g., for POST
requests).
headers: Record<string, string>
HTTP request headers.
timeoutInterval: number
The timeout interval in seconds for the request.
navigationType: "linkActivated" | "reload" | "backForward" | "formResubmitted" | "formSubmitted" | "other"
The context that triggered the navigation.
A Promise<boolean>
resolving to:
true
: allow the request to proceedfalse
: block the requestloadURL(url: string): Promise<boolean>
Loads a webpage by its URL.
Parameters:
url
: The full URL of the webpage to load.Returns: Promise<boolean>
— Resolves to true
if the load succeeds.
loadHTML(html: string, baseURL?: string): Promise<boolean>
Loads a web page using raw HTML content.
Parameters:
html
: The HTML string to render.baseURL
(optional): A base URL to resolve relative paths.Returns: Promise<boolean>
— Resolves to true
on successful load.
loadData(data: Data, mimeType: string, encoding: string, baseURL: string): Promise<boolean>
Loads web content from raw data.
Parameters:
data
: The binary content to load.mimeType
: MIME type of the content (e.g., "text/html"
).encoding
: Character encoding (e.g., "utf-8"
).baseURL
: Base URL for resolving relative URLs.Returns: Promise<boolean>
waitForLoad(): Promise<boolean>
Waits until the WebView has finished loading content.
Promise<boolean>
getHTML(): Promise<string | null>
Returns the current HTML content of the page.
Promise<string | null>
evaluateJavaScript<T = any>(javascript: string): Promise<T>
Evaluates a string of JavaScript in the context of the WebView.
Parameters:
javascript
: A JavaScript code string.Returns: Promise<T>
— The result of the script execution.
Example:
addScriptMessageHandler<P = any, R = any>(name: string, handler: (params?: P) => R): void
Installs a message handler callable from JavaScript in the web page.
Parameters:
name
: The message name. Must be unique.handler
: A callback that returns a result.Example:
present(options?: { fullscreen?: boolean, navigationTitle?: string }): Promise<void>
Presents the WebView in a modal sheet.
Options:
fullscreen
: If true
, displays the WebView in fullscreen mode.navigationTitle
: Optional title for the navigation bar.Returns: Promise<void>
canGoBack(): Promise<boolean>
Checks whether the WebView can go back in history.
canGoForward(): Promise<boolean>
Checks whether the WebView can go forward in history.
goBack(): Promise<boolean>
Navigates to the previous page in the history stack.
goForward(): Promise<boolean>
Navigates to the next page in the history stack.
reload(): Promise<void>
Reloads the current webpage.
dismiss(): void
Dismisses the WebView if currently presented.
dispose(): void
Disposes the WebView instance and releases resources.